package com.xmlt.core.template.instance;

import com.xmlt.core.template.TemplateParameter;

class InstanceUtils {
	static String getPath(AbstractInstance instance) {
		AbstractInstance parent = instance.getParent();
		if (parent == null) {
			// this is root
			return null;
		}
		TemplateParameter param = instance.getParameterReference();
		if (param != null && param.isMultiple() && !(instance instanceof MultiInstance)) {
			return getMultiPath(instance);
		}

		String parentPath = parent.getPath();
		String name = instance.getParameterReference().getName();
		return parentPath == null? name : parentPath + "." + name;
	}

	private static String getMultiPath(AbstractInstance instance) {
		AbstractInstance parent = instance.getParent();
		return parent.getPath() + "[" + ((MultiInstance) parent).indexOf(instance) + "]";
	}
}
